package com.springboot.uav.controller;


import com.springboot.uav.dto.LoginDto;
import com.springboot.uav.dto.RegisterDto;
import com.springboot.uav.entity.Result;
import com.springboot.uav.service.UserService;
import com.springboot.uav.util.JWTUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * Created by IntelliJ IDEA.
 * &#064;Author : Zys
 * &#064;create 2024/10/31 12:59
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Tag(name = "用户接口", description = "用户管理相关接口")
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    private JWTUtil jwtUtil;

    @PostMapping("/login")
    @Operation(summary = "用户登录接口", description = "用户通过用户名和密码进行登录,登录成功返回一个用户信息(含token)")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "登录成功 data返回一个token字符串",
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = Result.class),
                            examples = @ExampleObject(value = """
                                    {
                                        "code": "200",
                                        "msg": "登录成功",
                                        "data": {
                                            "userId": 2,
                                            "nickname": "贵凤英",
                                            "email": "hm1hs7_rot@139.com",
                                            "phoneNumber": "11467957309",
                                            "addressArray": [
                                                {
                                                    "addId": 1,
                                                    "address": "冬筑1326",
                                                    "addressType": 1
                                                },
                                                {
                                                    "addId": 2,
                                                    "address": "深圳大学南区菜鸟驿站",
                                                    "addressType": 2
                                                }
                                            ],
                                            "userGrade": 1,
                                            "balance": 48,
                                            "token": "AOSNDOIANDAIODOAIDONAODNADADWAHGFHFGH"
                                        }
                                    }""")
                    )),
            @ApiResponse(responseCode = "400", description = "用户名有误",
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = Result.class),
                            examples = @ExampleObject(value = """
                                    {
                                        "code": 400,
                                        "msg": "用户名有误",
                                        "data": null
                                    }"""))),
            @ApiResponse(responseCode = "401", description = "密码错误",
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = Result.class),
                            examples = @ExampleObject(value = """
                                    {
                                        "code": 401,
                                        "msg": "密码错误",
                                        "data": null
                                    }""")))
    })
    public Result login(@RequestBody LoginDto loginDto) {

        return userService.login(loginDto);
    }

    @PostMapping("/send")
    @Operation(summary = "用户注册的发送验证码接口", description = "用户通过用户名 邮箱和密码得到验证码")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "验证码发送成功"),
            @ApiResponse(responseCode = "400", description = "验证码失败 用户已经存在"),
    })
    public Result send(@RequestBody RegisterDto registerDto) {
        log.info("注册用户：{}", registerDto);
        return userService.send(registerDto);
    }

    @PostMapping("/register")
    @Operation(summary = "用户注册接口", description = "用户通过用户名 邮箱和密码,验证码进行注册")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "验证码发送成功"),
            @ApiResponse(responseCode = "400", description = "用户已经存在"),
            @ApiResponse(responseCode = "401", description = "验证码错误"),
            @ApiResponse(responseCode = "402", description = "验证码已过期请重新获取验证码"),
            @ApiResponse(responseCode = "403", description = "验证码为空，请先获取验证码"),
    })
    public Result register(@RequestBody RegisterDto registerDto) {
        log.info("注册用户：{}", registerDto);
        return userService.register(registerDto);
    }

    @PostMapping("/avatar")
    @Operation(summary = "用户修改头像接口", description = "用户通过新头像修改 通过token进行用户验证 返回头像路径")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "更新成功",
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = Result.class),
                            examples = @ExampleObject(value = """
                                    {
                                        "code": 200,
                                        "msg": "更新成功",
                                        "data": "https://web-llxx.oss-cn-shenzhen.aliyuncs.com/53685723-e696-42e5-ae11-af08f068ee71.png"
                                    }""")))
    })
    public Result updateAvatar(@RequestHeader("Authorization") String token, @RequestParam("file") MultipartFile file) {
        String username = jwtUtil.getUsernameFromToken(token);
        return userService.updateAvatar(username, file);

    }


}
